package com.weixing.mall.provider.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.dto.DepotExtraDto;
import com.weixing.mall.provider.model.excel.DepotSnExcelData;
import com.weixing.mall.provider.model.excel.DepotSnImportDataListener;
import com.weixing.mall.provider.service.IDepotExtraService;
import com.weixing.mall.provider.service.IProductStockDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 仓库扩展表
 * @author 0202
 * @date 2020-8-24
 */
@RestController
@RequestMapping("depotExtra")
@Api(value = "仓库扩展管理",tags = "仓库扩展管理")
public class DepotExtraController extends BaseController {
    @Autowired
    private IDepotExtraService depotExtraService;
    @Autowired
    private IProductStockDetailsService productStockDetailsService;

    @ApiOperation(value = "查询")
    @GetMapping("/{depotId}/{productId}")
    public Result list(@PathVariable("depotId") Long depotId,@PathVariable("productId")Long productId){
        return ResultUtil.success(depotExtraService.selectPage(initMpPage() ,depotId,productId));
    }

    /**
     * 通过产品查询仓库编码
     * @param productId
     * @return
     */
    @ApiOperation(value = "查询")
    @GetMapping("/{productId}")
    public Result list02(@PathVariable("productId")Long productId){
        return ResultUtil.success(depotExtraService.selectPage(initMpPage() ,null,productId));
    }

    @ApiOperation("新增或修改")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(DepotExtraDto ded){
        depotExtraService.saveOrUpdate(ded);
        return ResultUtil.success();
    }

    @ApiOperation("删除")
    @PostMapping("/del/{id}")
    public Result saveOrUpdate(@PathVariable("id") Long id){
        depotExtraService.delById(id);
        return ResultUtil.success();
    }

    @ApiOperation(value = "清空指定库存编码")
    @GetMapping("clearsDepotSn/{depotId}/{productId}")
    public Result clearsDepotSn(@PathVariable("depotId") Long depotId,@PathVariable("productId")Long productId){
        depotExtraService.delByCondition(depotId,productId,null);
        return ResultUtil.success();
    }

    @ApiOperation(value = "仓库编码批量导入删除")
    @PostMapping(value = "depotSnImport")
    public  Result depotSnImport(MultipartFile file){
        try {
            EasyExcel.read(file.getInputStream(), DepotSnExcelData.class,new DepotSnImportDataListener(depotExtraService,productStockDetailsService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
            return ResultUtil.error(e.getMessage());
        }
        return ResultUtil.success("success");
    }
}
